﻿using System;

using System.Collections.Generic;
using System.Text;
using ScannerApp.Utils;
using System.Collections;
using CodeBetter.Json;


namespace ScannerApp.ScannerWebserviceWrapper
{
    public class InStockOrderResult : BaseOrderResult
    {
        public InStockHeader recHeadGoodsInfo;

        public List<ReceivedGoods> recBodyGoodsList;

        public List<ReceivedGoods> recshGoodsList;

        public List<ReceivedGoods> recsjGoodsList;

        public List<ScannedInStockGoods> scannedGoods;

        public List<Product> products;

        private IList<MergedInBoxProduct> mMergedInBoxProducts;

        public int orderTotalPage;
        public int orderTotalCount;
        private int orderPageIndex;
        private int scanTotalPage;
        private int scanTotalCount;
        private int scanPageIndex;

        public int OrderPageIndex
        {
            get { return orderPageIndex; }
            set { orderPageIndex = value; }
        }

        public int ScanPageIndex
        {
            get { return scanPageIndex; }
            set { scanPageIndex = value; }
        }

        public int ScanTotalPage
        {
            get { return scanTotalPage; }
            set { scanTotalPage = value; }
        }

        public int ScanTotalCount
        {
            get { return scanTotalCount; }
            set { scanTotalCount = value; }
        }

        public int OrderTotalPage
        {
            get { return orderTotalPage; }
            set { orderTotalPage = value; }
        }

        public int OrderTotalCount
        {
            get { return orderTotalCount; }
            set { orderTotalCount = value; }
        }

        #region IScanProduct 成员
        public override bool IsProductCodeInOrder(string productCode)
        {
            foreach (ReceivedGoods rec in this.recBodyGoodsList)
            {
                if (rec.productcode.Equals(productCode))
                {
                    return true;
                }
            }
            return false;
        }


        public override bool IsProductEnablingQR(string productCode)
        {
            if (this.products != null)
            {
                foreach (Product product in this.products)
                {
                    if (product.code.Equals(productCode))
                    {
                        if (product.xlcode == true)
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        public override String GetHeaderInfo()
        {
            return String.Format("入库单号:{0}", recHeadGoodsInfo.AsnNumber) ;
        }

        public override bool IsQRInOrder(string barcode)
        {
            InStockSingleProductParameter param1 = new InStockSingleProductParameter();
            param1.ProductCode = barcode;
            String strResult = ScannerWebServiceProxy.Instance.inStockIsQRInOrder(Converter.Serialize(param1));
            BaseResult<InStockOrderResult> result = Converter.Deserialize<BaseResult<InStockOrderResult>>(strResult);

            return result.IsSucessful();

            /*
            foreach (ReceivedGoods rec in this.recBodyGoodsList)
            {
                if (barcode.Equals(rec.serialnumber))
                {
                    return true;
                }
            }
            return false;
            */
        }

        public override bool IfLotInOrder(String productCode, string lot)
        {
            bool foundLot = false;
            foreach (ReceivedGoods rec in this.recBodyGoodsList)
            {
                if (!rec.productcode.Equals(productCode))
                {
                    continue;
                }
                if (rec.batchnumber.Equals("") || rec.batchnumber.Equals(lot))
                {
                    foundLot = true;
                    break;
                }
            }
            return foundLot;
        }

        #endregion


        public bool IsFinishedStatus()
        {
            bool result = this.recHeadGoodsInfo.status.Equals("8")? true : false;
            return result;
        }

        public bool isEnablePutOnShelf = false;

        public bool IsEnablingPutOnShelf()
        {
            return isEnablePutOnShelf;
            /*
            if (this.recshGoodsList == null || this.recshGoodsList.Count == 0)
            {
                return false;
            }

            //这时候收货明细里的数据不为空， 如果上架明细为空， 返回真
            if (this.recsjGoodsList == null || this.recsjGoodsList.Count == 0)
            {
                return true;
            }

            if (this.recshGoodsList.Count > this.recsjGoodsList.Count)
            {
                return true;
            }

            //如果前面的简单判断无法决定，此时需要做详细的判断每个产品的数量
            foreach (ReceivedGoods shRec in this.recshGoodsList)
            {
                foreach (ReceivedGoods sjRec in this.recsjGoodsList)
                {
                    if (shRec.productcode.Equals(sjRec.productcode) && shRec.batchnumber.Equals(sjRec.batchnumber))
                    {
                        if (shRec.sh_count != sjRec.sj_count)
                        {
                            return true;
                        }
                        break;
                    }
                }
            }

            return false;
             * */
        }



        public IList<MergedInBoxProduct> GetMergedInBoxProducts()
        {
            Hashtable inventorys = new Hashtable();
            Hashtable directQRs = new Hashtable();
            IList<MergedInBoxProduct> result=new List<MergedInBoxProduct>();
            foreach (ReceivedGoods rec in this.recBodyGoodsList)
            {

                String key=rec.productcode+"|"+rec.batchnumber;
                if(!rec.serialnumber.Equals(""))
                {
                    key = rec.serialnumber;
                    directQRs.Add(key, rec);
                }

                if (!inventorys.ContainsKey(key))
                {
                    MergedInBoxProduct mergedRec = new MergedInBoxProduct();
                    mergedRec.serialNumber = rec.serialnumber;
                    mergedRec.inventoryCode = rec.productcode;
                    mergedRec.inventoryName = rec.productname;
                    mergedRec.batchNumber = rec.batchnumber;
                    mergedRec.totalCount = Convert.ToInt32(rec.rk_count);
                    mergedRec.totalRecCount = Convert.ToInt32(rec.sh_count);
                    mergedRec.totaFinishCount = Convert.ToInt32(rec.sj_count);

                    inventorys.Add(key, mergedRec);
                    result.Add(mergedRec);
                }
                else
                {
                    MergedInBoxProduct mergedRec = inventorys[key] as MergedInBoxProduct;
                    mergedRec.totalCount += Convert.ToInt32(rec.rk_count);
                }
                
            }
            /*
           
            //去除已收货产品
            if (this.recshGoodsList != null)
            {
                foreach (ReceivedGoods rec in this.recshGoodsList)
                {
                    String key = rec.productcode + "|" + rec.batchnumber;
                    if (directQRs.ContainsKey(rec.serialnumber))
                    {
                        key = rec.serialnumber;
                    }
                    //没有指定批号.
                    if(!inventorys.ContainsKey(key))
                    {
                        key = rec.productcode + "|";
                    }
                    MergedInBoxProduct mergedRec = inventorys[key] as MergedInBoxProduct;
                    if (mergedRec != null)
                    {
                        mergedRec.totalCount -= Convert.ToInt32(rec.sh_count);
                        mergedRec.totalRecCount += Convert.ToInt32(rec.sh_count);
                        if (mergedRec.totalCount == 0)
                        {
                            result.Remove(mergedRec);
                        }
                    }
                }
            }

            //去除已完成产品
            if (this.recsjGoodsList != null)
            {
                foreach (ReceivedGoods rec in this.recsjGoodsList)
                {
                    String key = rec.productcode + "|" + rec.batchnumber;
                    if (directQRs.ContainsKey(rec.serialnumber))
                    {
                        key = rec.serialnumber;
                    }
                    //没有指定批号.
                    if (!inventorys.ContainsKey(key))
                    {
                        key = rec.productcode + "|";
                    }

                    MergedInBoxProduct mergedRec = inventorys[key] as MergedInBoxProduct;
                    if (mergedRec != null)
                    {
                        //mergedRec.totalCount -=  Convert.ToInt32(rec.sj_count);
                        mergedRec.totaFinishCount += Convert.ToInt32(rec.sj_count);
                        
                        if (mergedRec.totalCount == 0)
                        {
                            result.Remove(mergedRec);
                        }
                    }
                }
            }

            //去除已扫描产品
            if (this.scannedGoods != null)
            {
                foreach (ScannedInStockGoods rec in this.scannedGoods)
                {
                    String key = rec.productCode + "|" + rec.batch;
                    if (directQRs.ContainsKey(rec.serialNumber))
                    {
                        key = rec.serialNumber;
                    }
                    //没有指定批号.
                    if (!inventorys.ContainsKey(key))
                    {
                        key = rec.productCode + "|";
                    }

                    MergedInBoxProduct mergedRec = inventorys[key] as MergedInBoxProduct;
                    if (mergedRec != null)
                    {
                        mergedRec.totalCount -= rec.amount;

                        if (mergedRec.totalCount == 0)
                        {
                            result.Remove(mergedRec);
                        }
                    }
                }
            }
            */

            mMergedInBoxProducts = result;
            return result;
        }

        public bool IsRepeatedScan(string orderCode, string barcode)
        {
            InStockSingleProductParameter param1 = new InStockSingleProductParameter();
            param1.InStockCode = orderCode;
            param1.ProductCode = barcode;
            String strResult = ScannerWebServiceProxy.Instance.inStockIsRepeatedScan(Converter.Serialize(param1));
            BaseResult<InStockOrderResult> result = Converter.Deserialize<BaseResult<InStockOrderResult>>(strResult);

            return result.IsSucessful();

            /*
            foreach (ScannedInStockGoods rec in this.scannedGoods)
            {
                if (rec.serialNumber.Equals(barcode))
                {
                    return true;
                }
            }

            if (this.recshGoodsList != null)
            {
                foreach (ReceivedGoods rec in this.recshGoodsList)
                {
                    if (rec.serialnumber.Equals(barcode))
                    {
                        return true;
                    }
                }
            }

            if (this.recsjGoodsList != null)
            {
                foreach (ReceivedGoods rec in this.recsjGoodsList)
                {
                    if (rec.serialnumber.Equals(barcode))
                    {
                        return true;
                    }
                }
            }

            return false;
             * */
        }

        public bool IfValidQuantity(String productCode, String lot, float quantity)
        {
            if (mMergedInBoxProducts == null)
            {
                return false;
            }

            foreach (MergedInBoxProduct rec in mMergedInBoxProducts)
            {
                //直接扫描QR,在ScannerBaseForm里已判断过是否QR属于此单号，在这里忽略。
                if (productCode.Equals(""))
                {
                    return true;
                }
                //如果产品不匹配，忽略
                if (!rec.inventoryCode.Equals(productCode))
                {
                    continue;
                }
                //如果不指定批号
                if (rec.batchNumber.Equals("") || rec.batchNumber.Equals(lot))
                {
                    if (rec.totalCount >= quantity)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

    }

    public class InStockHeader
    {
        // 未发现使用的item
       // private int id=0;

		public  int status;//状态
		
        //public  String statusName=String.Empty;//状态名称

        //public String externalDocNo = String.Empty;//外部单据号

        //public String outawb = String.Empty;//外部运单号

        public String asnnumber = String.Empty;//ASN编号

        public String AsnNumber
        {
            get { return asnnumber; }
            set { asnnumber = value; }
        }
		
        //public  int asnType;//ASN类型
		
        //public  int orderType;//订单类型
		
        public  int warehouseid;//仓库
		
        //public  int sousDocNumber;//来源单据编号
		
		
        //public  bool flagpay;//是否付款

        //public String GetDisplayString()
        //{
        //    String format = "编号:{0},状态:{1}";
        //    return string.Format(format, this.id, this.status);
        //}

        
    }


    public class ReceivedGoods
    {
        public int id ;

        public String productcode=String.Empty;//产品代码

        public String productname=String.Empty;//存货名称

        public String batchnumber=String.Empty;//批次

		public String validity=String.Empty;//失效期	

        public float rk_count;//入库数量

        public float sh_count;//收货数量
		
		public String sjDate=String.Empty;//上架时间

        public float sj_count;//上架数量

        public String carton_number=String.Empty;//箱号
		
		public String lineNumbers=String.Empty;//行号

        public String serialnumber=String.Empty;//序列号
        

        public bool IsSame(string barcode)
        {
            return this.productcode.Equals(barcode);
        }

        public bool IsEnablingQR()
        {
            return true;
        }
    }

    public class Product
    {
        public int id;
        public string number=String.Empty;
        public string code = String.Empty;
        public string name = String.Empty;
        public int prolineId;
        public int unit;
        public int proEnterprise;

        public bool batchManage;
        public bool xlcode;
        public bool enable;
        public bool isauthorized;

        public int purchtime;
        public int status;
        public int Invdays;
        public int abcstyle;
        public int safedays;

        public bool isDeleted;

    }

    public class ScannedInStockGoods
    {
        public int id;

        [DataTableFieldAttribute(HeaderText = "序列号")]
        public String serialNumber=String.Empty;

        public int deliverygoodsHeadId;

    	
        [DataTableFieldAttribute(HeaderText = "产品代码")]
        public String productCode = String.Empty;

        [DataTableFieldAttribute(HeaderText = "批次")]
        public String batch = String.Empty;

        [DataTableFieldAttribute(HeaderText = "数量")]
        public int amount;

        [DataTableFieldAttribute(HeaderText = "货位号")]
        public String positionCode = String.Empty;

        public int status;

    }

    public class MergedInBoxProduct
    {

        [DataTableFieldAttribute(HeaderText = "序列号")]
        public String serialNumber = String.Empty;

        [DataTableFieldAttribute(HeaderText="产品代码")]
	    public String inventoryCode=string.Empty;//产品代码

		public String inventoryName=string.Empty;//存货名称

        [DataTableFieldAttribute(HeaderText = "批次")]
	    public String batchNumber=string.Empty;//批号

        [DataTableFieldAttribute(HeaderText = "未扫描数量")]
        public int totalCount = 0;


        [DataTableFieldAttribute(HeaderText = "已收货数量")]
        public int totalRecCount = 0;

        [DataTableFieldAttribute(HeaderText = "已完成数量")]
        public int totaFinishCount = 0;
    }
}
